﻿var CurrentFlowName = {};
var CurrentNode = {};
Ext.onReady(function () {
    PageSafe();

    //人员列表
    var Rolestore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.RoleView', 'GetRoles'),
        fields: ['PID', 'RoleName'],
        root: 'ResponseObject'
    });
    Rolestore.load();

    var flownamestore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Flow.FlowView', 'GetBaseFlow'),
        root: 'ResponseObject',
        fields: ['PID', 'FlowN', 'Creater', 'LastUpdate', 'NTime', 'CreateName.UserName'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var flownamesm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = flownamegrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentFlowName = k[0].json;
                    nodestore.load();
                }
            },
            rowdeselect: function () {
                CurrentFlowName = {};
            }
        }
    });
    var flownamegrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        autoHeight: true,
        sm: flownamesm,
        loadMask: true, //加载
        columns: [
       flownamesm,
       { id: 'FlowN', header: '流程名称', width: 100, dataIndex: 'FlowN', sortable: true },
       { id: 'CreateName.UserName', header: '发布人', width: 80, dataIndex: 'CreateName.UserName', sortable: true },
       { id: 'NTime', header: '发布时间', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: flownamestore
    });


    //节点流程
    var nodestore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Flow.FlowView', 'GetNodeFlow'),
        root: 'ResponseObject',
        fields: ['PID', 'FlowID', 'Creater', 'LastUpdate', 'NTime', 'CreateName.UserName', 'NodeName', 'RoleID', 'UserRole.RoleName', 'SortOrder'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                re.params.id = CurrentFlowName.PID;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var nodesm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = nodegrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentNode = k[0].json;
                }
            },
            rowdeselect: function () {
                CurrentNode = {};
            }
        }
    });
    var nodegrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        autoHeight: true,
        sm: nodesm,
        loadMask: true, //加载
        columns: [
       nodesm,
       { id: 'NodeName', header: '节点名称', width: 100, dataIndex: 'NodeName', sortable: true },
       { id: 'UserRole.RoleName', header: '负责角色', width: 100, dataIndex: 'UserRole.RoleName', sortable: true },
       { id: 'CreateName.UserName', header: '发布人', width: 80, dataIndex: 'CreateName.UserName', sortable: true },
       { id: 'SortOrder', header: '序号', width: 80, dataIndex: 'SortOrder', sortable: true },
       { id: 'NTime', header: '发布时间', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: nodestore
    });


    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'west',
            width: 300,
            title: '流程定义',
            style: 'padding:2px',
            iconCls: 'flow-icon',
            tbar: [{ text: '新增流程名称', iconCls: 'add-icon', handler: function () {
                var addwin = new Ext.Window({
                    width: 300,
                    resizable: false,
                    modal: true,
                    height: 160,
                    title: '新增流程',
                    iconCls: 'flow-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '流程名称',
                        width: 100,
                        id: 'flowname',
                        name: 'flowname'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("flowname").getValue() == "") { Ext.Msg.alert("提示信息", "请输入流程名称"); return; }


                        var re = new JsonRequest();

                        re.params.info = {
                            FlowN: Ext.getCmp("flowname").getValue()
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('Flow.FlowView', 'AddBaseFlow'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("提示信息", "流程名称增加成功");
                                }
                                else {
                                    Ext.Msg.alert("提示信息", "流程名称增加失败，请联系管理员");
                                }
                                flownamestore.load();
                                addwin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { addwin.close(); } }]
                }).show();
            }
            }, { text: '删除流程名称', iconCls: 'del-icon', handler: function () {
                if (CurrentFlowName.PID == null) { Ext.Msg.alert("提示信息", "请选择需要删除的流程"); return; }
                var re = new JsonRequest();
                re.params.info = CurrentFlowName;
                Ext.Ajax.request({
                    url: Ext.getClassUrl('Flow.FlowView', 'DelBaseFlow'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            Ext.Msg.alert("提示信息", "流程名称删除成功");
                        }
                        else {
                            Ext.Msg.alert("提示信息", "流程名称删除失败，请联系管理员");
                        }
                        flownamestore.load();
                    }
                });
            }
            }],
            items: flownamegrid,
            listeners: {
                render: function () {
                    flownamestore.load();
                }
            }
        }, {
            region: 'center',
            style: 'padding:2px',
            title: '流程节点',
            iconCls: 'flow1-icon',
            tbar: [{ text: '新增流程节点', iconCls: 'add-icon', handler: function () {
                var addnodewin = new Ext.Window({
                    width: 300,
                    resizable: false,
                    modal: true,
                    height: 160,
                    title: '新增流程节点',
                    iconCls: 'flow-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '节点名称',
                        width: 100,
                        id: 'nodename',
                        name: 'nodename'
                    }, {
                        fieldLabel: '节点负责角色',
                        width: 100,
                        xtype: 'combo',
                        id: 'noderole',
                        displayField: 'RoleName',
                        valueField: 'PID',
                        selectOnFocus: true,
                        triggerAction: 'all',
                        store: Rolestore
                    }, {
                        fieldLabel: '节点顺序',
                        width: 50,
                        id: 'sortorder',
                        name: 'sortorder'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (CurrentFlowName.PID == null) { Ext.Msg.alert("提示信息", "请选择一个流程增加节点"); return; }
                        if (Ext.getCmp("nodename").getValue() == "") { Ext.Msg.alert("提示信息", "请输入流程节点名称"); return; }
                        if (Ext.getCmp("noderole").getValue() == "") { Ext.Msg.alert("提示信息", "请选择流程节点负责角色"); return; }
                        if (Ext.getCmp("sortorder").getValue() == "") { Ext.Msg.alert("提示信息", "请输入节点顺序"); return; }


                        var re = new JsonRequest();

                        re.params.info = {
                            FlowID: CurrentFlowName.PID,
                            NodeName: Ext.getCmp("nodename").getValue(),
                            RoleID: Ext.getCmp("noderole").getValue(),
                            SortOrder: Ext.getCmp("sortorder").getValue()
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('Flow.FlowView', 'AddNodeFlow'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("提示信息", "流程节点增加成功");
                                }
                                else {
                                    Ext.Msg.alert("提示信息", "流程节点增加失败，请联系管理员");
                                }
                                nodestore.load();
                                addnodewin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { addnodewin.close(); } }]
                }).show();
            }
            }, { text: '删除流程节点', iconCls: 'del-icon', handler: function () {
                if (CurrentNode.PID == null) { Ext.Msg.alert("提示信息", "请选择需要删除的流程节点"); return; }
                var re = new JsonRequest();
                re.params.info = CurrentNode;
                Ext.Ajax.request({
                    url: Ext.getClassUrl('Flow.FlowView', 'DelNodeFlow'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            Ext.Msg.alert("提示信息", "流程节点删除成功");
                        }
                        else {
                            Ext.Msg.alert("提示信息", "流程节点删除失败，请联系管理员");
                        }
                        nodestore.load();
                    }
                });
            }
            }],
            items: nodegrid,
            listeners: {
                render: function () {
                    nodestore.load();
                }
            }
        }]
    });
});